!pr1
Corrections to Line Number Cross Reference..........Bill Morgan

Allen Miller just called up from Hong Kong (at 3:30 AM his time!) to report a problem with the Line Number Cross Reference program in the August issue.  It seems that as published it only prints out the first line number list in each chain.  The troublemaker is line 4560, which says BNE .1.  Well .1 is the next line, so the routine is always exiting after only one pass.  Line 4560 should read BNE PRINT.CHAIN, to go back to the beginning rather than on to the end.

Then Chuck Welman called to point out yet another problem.  It seems that an undefined line number greater than the last line of the program caused LCR to head off into the wilderness.  When I investigated this one it proceeded to get even stranger.  LCR would hang only if the undefined line number was greater than 19668!  Less than 19668 came out just right, and equal to 19668 worked, but LCR mistakenly said the line was defined.  Now here was a real creepy crawler of a bug!

Well the problem turned out to be in the CHECK.DEFINITION routine.  Here are the offending lines:

!lm+5
4790 .4    LDY #0
4800       LDA (PNTR),Y    lo-byte of next line address
4810       PHA
4820       INY
4830       LDA (PNTR),Y    and hi-byte
4840       STA PNTR+1
4850       PLA
4860       STA PNTR
4870       JMP CHECK.DEFINITION
!lm-5

This code is called when CHECK.DEFINITION wants to get the next line of the Applesoft program.  The trouble comes up because there is no check for end-of-program.  Sooner or later we come to the zero bytes that mark the end, load up PNTR with zeroes, and go back to CHECK.DEFINITION to try what seems to be the next line.  That routine then compares the line number we are checking to the contents of locations 2 and 3 of memory, which Applesoft has loaded with D4 and 4C.  Now $4CD4 equals 19668, so that's where that funny number came from!

Here is a slightly rearranged, working version of lines 4790-4870.  Note that we have reversed the hi-lo byte sequence and added a check for a zero hi-byte:

!lm+5
4790 .4    LDY #1
4800       LDA (PNTR),Y    hi-byte of next line address
4805       BEQ .2          end of program?
4810       PHA
4820       DEY
4830       LDA (PNTR),Y    and lo-byte
4840       STA PNTR
4850       PLA
4860       STA PNTR+1
4870       JMP CHECK.DEFINITION
